﻿Imports FastReport.Report
Public Class frmPrintDrawForm
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
    Public Report_Name As String
    Public Primary_Key As Integer
    Private Sub frmPrintDrawForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Try
            Strsql = "SELECT pr_header.doc_id,"
            Strsql = Strsql & " pr_header.doc_no,"
            Strsql = Strsql & " pr_header.doc_date,"
            Strsql = Strsql & " pr_header.doc_department,"
            Strsql = Strsql & " pr_header.doc_remarks,"
            Strsql = Strsql & " pr_header.doc_recevie_date,"
            Strsql = Strsql & " pr_header.doc_lastupdate,"
            Strsql = Strsql & " pr_header.doc_status,"
            Strsql = Strsql & " pr_header.doc_type,"
            Strsql = Strsql & " pr_header.doc_place,"
            Strsql = Strsql & " pr_header.doc_staff,"
            Strsql = Strsql & " departments.dep_id,"
            Strsql = Strsql & " departments.dep_name,"
            Strsql = Strsql & " pr_detail.*,"
            Strsql = Strsql & " item_type_id,itemname,"
            Strsql = Strsql & " sum(pr_detail.drawqty) as drawqty"
            Strsql = Strsql & " from(pr_header)"
            Strsql = Strsql & " left join pr_detail on pr_header.doc_id = pr_detail.doc_head_id"
            Strsql = Strsql & " left join itemtype on item_type_id = itemnumber"
            Strsql = Strsql & " left join departments on pr_header.doc_department = departments.dep_id"
            Strsql = Strsql & " where(doc_id = " & Primary_Key & ")"
            Strsql = Strsql & " group by itemname"
            Strsql = Strsql & " order by doc_rowid"
            Dim DS_Documents As New DataSet
            DS_Documents = _mysql.GetMYSQLDataset(Strsql, "IDM", "reporttemplate")
            Dim TotalRow As Integer = DS_Documents.Tables(0).Rows.Count
            Dim AddRow As Integer
            Select Report_Name
                Case "ใบขออนุมัติซื้อ.frx"

                    If TotalRow <> 16 Then
                        AddRow = 16 - TotalRow
                        For i = 1 To AddRow
                            Dim DsNewRow As DataRow
                            DsNewRow = DS_Documents.Tables(0).NewRow
                            DsNewRow.Item("doc_id") = Primary_Key
                            DsNewRow.Item("doc_rowid") = i + 1

                            DS_Documents.Tables(0).Rows.Add(DsNewRow)

                        Next
                    End If
                Case "ใบขออนุมัติซ๋อมหน่วยงานไอที.frx"
                    If TotalRow <> 5 Then
                        AddRow = 5 - TotalRow
                        For i = 1 To AddRow
                            Dim DsNewRow As DataRow
                            DsNewRow = DS_Documents.Tables(0).NewRow
                            DsNewRow.Item("doc_head_id") = Primary_Key
                            DsNewRow.Item("doc_rowid") = i + 1

                            DS_Documents.Tables(0).Rows.Add(DsNewRow)

                        Next
                    End If
            End Select


           





            Dim report1 As New FastReport.Report

            report1.Preview = PreviewControl2
            report1.Load(My.Settings.ReportPath & Report_Name)


            report1.AutoFillDataSet = True

            report1.RegisterData(DS_Documents)

            report1.GetDataSource("reporttemplate").Enabled = True

            report1.Refresh()
            report1.Show()




        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "BtnPrint_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try

    
    End Sub



End Class